package com.yuand.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuand.entity.MenuTree;
import com.yuand.entity.SysMenu;
import com.yuand.entity.SysRole;
import com.yuand.entity.SysUser;
import com.yuand.service.SysMenuService;
import com.yuand.service.UserService;
import com.yuand.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author yuandong
 * @since 2022-01-15
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private UserService userService;


    /**
     * 查询菜单列表
     *
     * @param menu
     * @return
     */
    @GetMapping("/list")
    public ResponseResult list(SysMenu menu) {
        List<SysMenu> list = sysMenuService.selectMenuList(menu, getUserId());
        return new ResponseResult(0, "操作成功", list);
    }


    /**
     * 查询当前用户对应的菜单(并构建树形结构,不包括按钮,不包括根目录)
     * 在这里使用Principla接收参数，这样在vue查询用户拥有的菜单的时候，不需要用户的id了
     * 侧边栏
     *
     * @return
     */

    @GetMapping("/tree")
    public ResponseResult getMenuTree(Principal principal) {

        SysUser user = userService.selectByUsername(principal.getName());

        //侧边栏的查询  查询所有(和上面的selectMenuList方法是重载方法，只是参数不一样)
        List<SysMenu> menuAll = sysMenuService.selectMenuList(user.getUserId());

        //过滤到菜单级别，排除按钮
        List<SysMenu> menus = menuAll.stream().filter(e -> !"3".equals(e.getMenuType())).collect(Collectors.toList());

        MenuTree menuTree = new MenuTree(menus);
        List<SysMenu> menuList = menuTree.builTree();
        return new ResponseResult(0, "操作成功", menuList);
    }

    /**
     * 根据用户获取该对应的所有菜单（包括按钮）
     * (角色新增的时候展现所有菜单权限时使用)
     *
     * @return
     */
    @GetMapping("/menuTree")
    public ResponseResult getUserMenuTree(Principal principal, SysMenu sysMenu) {
        SysUser user = userService.selectByUsername(principal.getName());
        //只查询该用户对应的菜单(包括按钮)
        List<SysMenu> menus = sysMenuService.selectMenuList(sysMenu, user.getUserId());

        MenuTree menuTree = new MenuTree(menus);
        List<SysMenu> menuList = menuTree.builTree();
        return new ResponseResult(0, "操作成功", menuList);
    }


    /**
     * 保存菜单
     *
     * @param sysMenu
     * @return
     */
    @PostMapping("/addMenu")
    public ResponseResult addMenu(@RequestBody SysMenu sysMenu) {
        //1:先判断菜单名称是否唯一 sql判断菜单名和父id同时相等
        boolean flag = this.sysMenuService.checkMenuNameUnique(sysMenu);
        if (flag) {
            return new ResponseResult(500, "菜单名重复");
        }

        //2:正常保存菜单
        int row = this.sysMenuService.addMenu(sysMenu);
        if (row > 0) {
            return new ResponseResult(0, "保存成功");
        } else {
            return new ResponseResult(500, "保存失败");
        }
    }


    /**
     * 修改菜单
     *
     * @param sysMenu
     * @return
     */
    @PostMapping("/updateMenu")
    public ResponseResult updateMenu(@Validated @RequestBody SysMenu sysMenu) {
        //1:先判断菜单名称是否唯一 sql判断菜单名和父id同时相等
        boolean flag = this.sysMenuService.checkMenuNameUnique(sysMenu);
        if (flag) {
            return new ResponseResult(500, "菜单名重复");
        }

        if (sysMenu.getMenuId().equals(sysMenu.getParentId())) {
            return new ResponseResult(500, "修改失败，上级菜单不能选择自己");
        }
        //2: 正常修改菜单
        int rows = this.sysMenuService.updateMenu(sysMenu);

        return rows > 0 ? new ResponseResult(0, "修改成功") : new ResponseResult(0, "修改失败");

    }

    /**
     * 删除菜单
     *
     * @param menuId
     * @return
     */
    @DeleteMapping("/delMenu/{menuId}")
    public ResponseResult delMenu(@PathVariable("menuId") Long menuId) {

        //1:存在子菜单的不能删除
        boolean flag = this.sysMenuService.hasChildByMenuId(menuId);
        if (flag) {
            return new ResponseResult(500, "存在子菜单,不允许删除");
        }

        //2:已经关联角色的不能删除
        boolean result = this.sysMenuService.checkMenuExistRole(menuId);
        if (result) {
            return new ResponseResult(500, "该菜单已关联角色,不允许删除");
        }

        //3:正常删除
        int mark = this.sysMenuService.delMenu(menuId);

        return mark > 0 ? new ResponseResult(0, "删除成功") : new ResponseResult(500, "删除失败");
    }


    /**
     * 根据menuId查询菜单
     *
     * @param menuId
     * @return
     */
    @GetMapping("/selectByMenuId/{menuId}")
    public ResponseResult selectByMenuId(@PathVariable Long menuId) {
        SysMenu sysMenu = this.sysMenuService.selectByMenuId(menuId);
        return new ResponseResult(0, sysMenu);
    }


}
